{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "06e4b714-df3a-4cdb-bbd3-b842febbb6b2",
   "metadata": {},
   "source": [
    "Detailed description of run configuration could be found [here](../nablaDFT/README.md)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6b7663d-5750-41c8-b433-3f2bdbe9258c",
   "metadata": {},
   "source": [
    "## Test example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2000a6a4-1e61-495e-b9db-4bd1db4689c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# model test example config\n",
    "!cat ../config/gemnet-oc_test.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed4b204b-f4c8-4993-a5fb-e65ae77ac69c",
   "metadata": {},
   "outputs": [],
   "source": [
    "!python ../run.py --config-name gemnet-oc_test.yaml"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d7edabf-4253-4ab7-8fd7-3eeb6c51c220",
   "metadata": {},
   "source": [
    "## Inference on another dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffb9d8f6-b4c6-4528-ae2a-0bdc56f8020b",
   "metadata": {},
   "source": [
    "For CLI-run please use example from the root of the repository:\n",
    "```bash\n",
    "python run.py --config-name gemnet-oc_predict.yaml\n",
    "```\n",
    "\n",
    "Detailed description could be found in [README](../nablaDFT/README.md)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7108dd94-065b-4436-a11f-7afe70f1e943",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import hydra\n",
    "import pytorch_lightning as pl\n",
    "import torch\n",
    "from ase.db import connect\n",
    "from nablaDFT import model_registry\n",
    "from nablaDFT.dataset import PyGNablaDFTDataModule\n",
    "from nablaDFT.pipelines import predict\n",
    "from omegaconf import OmegaConf"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "109f9ba4-b94d-4f4d-a1e7-53d825dfca7e",
   "metadata": {},
   "source": [
    "### Paths and args"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a1483ab-8614-466c-8bea-d9ae91fa8b1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_args = {\n",
    "    \"root\": \"./datasets/nablaDFT/test\",\n",
    "    \"dataset_name\": \"dataset_test_conformations_tiny\",\n",
    "    \"batch_size\": 4,\n",
    "    \"num_workers\": 2,\n",
    "}\n",
    "if torch.cuda.is_available():\n",
    "    accelerator = \"gpu\"\n",
    "    devices = 1\n",
    "else:\n",
    "    accelerator = \"cpu\"\n",
    "    devices = None"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9e362a9-ea34-40eb-857b-e25731f11a44",
   "metadata": {},
   "source": [
    "### Instantiate dataset and load model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3be63556-0779-4a11-86e5-9b388ba06131",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = model_registry.get_pretrained_model(\"lightning\", \"GemNet-OC_train_large\")\n",
    "datamodule = PyGNablaDFTDataModule(**data_args)\n",
    "trainer = pl.Trainer(accelerator=accelerator, devices=devices)\n",
    "trainer.logger = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cfbdc3f-50de-4621-8287-376873ce85bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "predict(trainer, model, datamodule, ckpt_path=None, model_name=\"GemNet-OC-large\", output_dir=\"./predictions\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9512d009-46f0-4f7b-856f-5a82210f31a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "db = connect(\"./predictions/GemNet-OC-large_dataset_test_conformations_tiny.db\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "859a2658-e42d-428c-9b77-1071a15cda79",
   "metadata": {},
   "outputs": [],
   "source": [
    "!ls predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "079b9730-7523-4212-8931-d80250ed35bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(db)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bb7ea4b-d2ce-42a3-a9fc-73209417d12a",
   "metadata": {},
   "outputs": [],
   "source": [
    "row = db.get(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4122518c-9084-4412-9204-b6fed9eac17a",
   "metadata": {},
   "outputs": [],
   "source": [
    "energy, forces = row.data[\"energy_pred\"], row.data[\"forces_pred\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "345ea902-17ab-4692-8ab9-c1c421be1444",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f\"Predicted energy: {energy}\")\n",
    "print(\"Predicted interatomic forces:\")\n",
    "print(forces)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69862f1e-266c-41c6-88ee-56c52da320eb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
